package smcms.scheduled.config;

import feign.Feign;
import feign.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignConfig {
    @Autowired
    private RedisTemplate redisTemplate;
    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
                if (requestTemplate.url().equals("/login")) {
                    return;
                }
                String zhihuiyun = (String)redisTemplate.opsForValue().get("zhihuiyun");
                // 将token信息放入header中
                requestTemplate.header("token", zhihuiyun);
        };
    }
}
